package wci.frontend.pascal.tokens;

import wci.frontend.*;
import wci.frontend.pascal.*;

import static wci.frontend.pascal.PascalTokenType.*;

/**
 * <h1>PascalWordToken</h1>
 *
 * <p> Pascal单词token (identifiers即ID 和 关键字).</p>
 *
 * <p>Copyright (c) 2009 by Ronald Mak</p>
 * <p>For instructional purposes only.  No warranties.</p>
 */
public class PascalWordToken extends PascalToken
{
    public PascalWordToken(Source source)
        throws Exception
    {
        super(source);
    }
    protected void extract()
        throws Exception
    {
        StringBuilder textBuffer = new StringBuilder();
        char currentChar = currentChar();
        
        while (Character.isLetterOrDigit(currentChar)) {
            textBuffer.append(currentChar);
            currentChar = nextChar();
        }

        text = textBuffer.toString();

        // 是关键字还是普通ID?
        type = (RESERVED_WORDS.contains(text.toLowerCase()))
               ? PascalTokenType.valueOf(text.toUpperCase())  // reserved word
               : IDENTIFIER;                                  // identifier
    }
}
